/**.
 *
 */
package com.ww.companyCabinetSystem.controller;

import java.lang.reflect.Type;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.ww.companyCabinetSystem.common.Response;
import com.ww.companyCabinetSystem.common.StatusCode;
import com.ww.companyCabinetSystem.model.bo.MAdminBo;
import com.ww.companyCabinetSystem.utils.JsonUtil;


/**.
 * com.ww.companyCabinetSystem.controller
 * TODO
 * @author veloster
 * 上午11:53:46
 */
public class BaseController {
    /**.
     * log日志
     */
    protected Logger log = LogManager.getLogger(BaseController.class);
    
    /**
     * . 返回成功.
     * 
     * @param content
     *            返回内容
     * @return .
     */
    public String toSuccess(Object content) {
        Response<Object> responseObject = new Response<Object>(StatusCode.SUCCESS, null, content);
        return JsonUtil.toJson(responseObject);
    }

    /**
     * . 返回成功.
     * 
     * @return .
     */
    public String toSuccess() {
        Response<Object> responseObject = new Response<Object>(StatusCode.SUCCESS, null, null);
        return JsonUtil.toJson(responseObject);
    }

    /**
     * . 成功.
     * 
     * @param content
     *            内容
     * @param message
     *            返回信息
     * @return FDEF FED
     */
    public String toSuccess(Object content, String message) {
        Response<Object> responseObject = new Response<Object>(StatusCode.SUCCESS, message, content);
        return JsonUtil.toJson(responseObject);
    }

    /**
     * . 失败
     * 
     * @return FRD RFED
     */
    public String toFailure() {
        return JsonUtil.toJson(new Response<Object>(StatusCode.ERROR, null, null));
    }

    /**
     * . 失败
     * 
     * @param message
     *            失败消息
     * @return FRERF ERWR
     */
    public String toFailure(String message) {
        return JsonUtil.toJson(new Response<Object>(StatusCode.ERROR, message, null));
    }

    /**
     * . 失败
     * 
     * @param code
     *            失败状态码(状态码来自com.better517na.commons.StatusCode或者程序自定义状态码)
     * @param message
     *            失败消息
     * @return FDE RFE
     */
    public String toFailure(int code, String message) {
        return JsonUtil.toJson(new Response<Object>(code, message, null));
    }

    /**
     * . 通用返回
     * 
     * @param code
     *            状态码
     * @param message
     *            信息
     * @param content
     *            内容
     * @return RF ER R
     */
    public String toJson(int code, String message, Object content) {
        return JsonUtil.toJson(new Response<Object>(code, message, content));
    }

    /**
     * .
     * @param json
     * @param type
     * @return
     */
    public Response<Object> toObject(String json, Type type) {
        return JsonUtil.toObject(json, type);
    }

    /**
     * .
     * @param json
     * @param cla
     * @return
     */
    public <T> T toObject(String json, Class<T> cla) {
        return JsonUtil.toObject(json, cla);
    }

    /**
     * . . 与以前的没有状态码的系统兼容
     */
    public String toJson(Object content) {
        if (content == null) {
            return "";
        }
        return JsonUtil.toJson(content);
    }
    
}
